<template>
  <ContentWrap>
    <fromTitle title="检验信息" :hasPermi="'qis:check-result:query'" :queryParams="queryParams" @search="getList" @reset="resetQuery" />

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入样品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="样品编号" prop="sampleNum" label-width="100">
        <el-input
          v-model="queryParams.sampleNum"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="承检机构" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入承检机构"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="检验状态" prop="checkState">
        <el-select
          v-model="queryParams.checkState"
          placeholder="请选择检验状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="item of statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value.toString()"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="抽样日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          placeholder="请选择抽样日期"
          format="YYYY-MM-DD"
          type="daterange"
          value-format="YYYY-MM-DD"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="检测开始日期" label-width="100" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime1"
          placeholder="请选择检验开始日期"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          class="!w-240px"
        />
      </el-form-item>

      <!-- <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

      </el-form-item> -->
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="info" v-hasPermi="['qis:check-result:expor']"  @click="handleExport">导出</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="样品名称" align="center" prop="sampleName">
        <template #default="scope">
          <el-button type="text" @click="handleDetail(scope.row)">{{
            scope.row.sampleName
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="样品编号" align="center" prop="sampleNum" />
      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="执行标准" align="center" prop="executiveStandard" />
      <el-table-column label="抽样地点" align="center" prop="samplingSite" />
      <el-table-column label="抽样日期" align="center" prop="samplingDate" />
      <el-table-column label="检测开始日期" align="center" prop="checkStartTime" />
      <el-table-column label="检测结束日期" align="center" prop="checkEndTime" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="承检机构" align="center" prop="orgName" />
      <el-table-column label="检验状态" align="center" prop="checkStateString" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrganizationQualificationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import {
  OrganizationQualificationApi,
  OrganizationQualificationVO
} from '@/api/qis/organizationqualification'
import OrganizationQualificationForm from './OrganizationQualificationForm.vue'
import { useRouter } from 'vue-router'
import { SampleInformationApi } from '@/api/qis/sampleinformation'
import { getDictDataPage } from '@/api/system/dict/dict.data.ts'
/** 质检平台-承检资质 列表 */
defineOptions({ name: 'OrganizationQualification' })

const arrayList = ref([])

const handleSelect = (row) => {
  // console.log('删除指标要求', row)
  if (row.selected) {
    arrayList.value.push(row.id)
    console.log(arrayList.value, 'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(item => item !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }
}

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrganizationQualificationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: 1
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  queryParams.samplingStartDate = queryParams.createTime ? queryParams.createTime[0] : ''
  queryParams.samplingEndDate = queryParams.createTime ? queryParams.createTime[1] : ''
  queryParams.checkFilterStartTime = queryParams.createTime1 ? queryParams.createTime1[0] : ''
  queryParams.checkFilterEndTime = queryParams.createTime1 ? queryParams.createTime1[1] : ''
  try {
    const data = await SampleInformationApi.getSampleInformationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const router = useRouter()
const handleDetail = (row: any) => {
  router.push({
    path: '/inspection-info-management/InspectionInfo/add',
    query: {
      id: row.id
    }
  })
}

const statusList = ref([])
// 获取检验状态字典
const getDictDataPageApi = async () => {
  const res = await getDictDataPage({
    dictType: 'check_state'
  })
  statusList.value = res.list
  console.log(res, '状态字典')
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.createTime1 = undefined
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrganizationQualificationApi.deleteOrganizationQualification(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    if (arrayList.value.length > 0) {
      // 导出的二次确认
      const idsArr = arrayList.value.join(';')
      await message.exportConfirm()
      // 发起导出
      exportLoading.value = true
      const data = await SampleInformationApi.exportSampleInformation({
        ids: idsArr,
        ...queryParams
      })
      download.excel(data, '质检平台-检验信息.xls')
    } else {
      // 导出的二次确认
      await message.exportConfirm()
      // 发起导出
      exportLoading.value = true
      const data = await SampleInformationApi.exportSampleInformation(queryParams)
      download.excel(data, '质检平台-检验信息.xls')
    }
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()

  getDictDataPageApi()
})

onActivated(()=>{
  console.log('onActivated')
  getList()

  getDictDataPageApi()
})

</script>

<style lang="scss" scoped>
.table-btn {
  padding: 10px 50px;
}
</style>
